// Config.cpp: implementation of the CConfig class.
//
//////////////////////////////////////////////////////////////////////

//#include "stdafx.h"
#include "stdafx.h"
#include "Config.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif



//! 构造、析构
// Construction/Destruction
CConfig::CConfig()
	: m_fileName(_T(""))
{
}

CConfig::CConfig(CString strFile)
	: m_fileName(strFile)
{
}

CConfig::~CConfig()
{
}

void CConfig::SetCfgFile(CString strFile)
{
	m_fileName = strFile;
}

CString CConfig::GetCfgFile()
{
	return m_fileName;
}

CString CConfig::GetKeyValue(
	const CString& appName, 
	const CString& keySource, const CString& defaultValue)
{
	// 获取值
	// get value
	TCHAR val[MAX_VALUE_LEN] = {0};
	GetPrivateProfileString(
		appName, keySource, defaultValue, 
		val, sizeof(val), m_fileName);
	
	//! 结果
	// result
	CString strVal(_T(""));
	strVal.Format(_T("%s"), val);
	return strVal;
}

BOOL CConfig::IsEnable(
	const CString& appName, 
	const CString& keySource, BOOL bDefault)
{
	int nValue = GetKeyInt(appName, keySource, bDefault);	
	return (0 == nValue ? FALSE : TRUE);
}

int CConfig::GetKeyInt(
	const CString& appName,
	const CString& keySource, int nDefault)
{
	//! 获取值
	// get value
	int nRet = GetPrivateProfileInt(
		appName, keySource, nDefault, m_fileName);

	//! 结果
	// result
	return nRet;
}

double CConfig::GetKeyDouble(
	const CString& appName,
	const CString& keySource, double fDefault)
{
	// 获取值
	// getValue
	CString strDefault(_T(""));
	strDefault.Format(_T("%.2f"), fDefault);
	CString strRes = GetKeyValue(appName, keySource, strDefault);

	//! 结果
	// result
#ifdef _UNICODE
	double fRet = _wtof(strRes.GetBuffer(0));
#else
	double fRet = atof(strRes.GetBuffer(0));
#endif
	
	return fRet;
}

BOOL CConfig::SetKeyValue(
	const CString& appName,
	const CString& keySource, const CString& strValue)
{
	return WritePrivateProfileString(
		appName, keySource, strValue, m_fileName);
}

BOOL CConfig::SetEnable(
	const CString& appName, 
	const CString& keySource, BOOL bValue)
{
	return SetKeyInt(appName, keySource, bValue);
}

BOOL CConfig::SetKeyInt(
	const CString& appName,
	const CString& keySource, int nValue)
{
	CString strValue(_T(""));
	strValue.Format(_T("%d"), nValue);
	return SetKeyValue(appName, keySource, strValue);
}

BOOL CConfig::SetKeyDouble(
	const CString& appName,
	const CString& keySource, double fValue, int nFrac)
{
	CString strFormat(_T(""));
	strFormat.Format(_T("%%.%df"), nFrac);
	CString strValue(_T(""));
	strValue.Format(strFormat, fValue);
	return SetKeyValue(appName, keySource, strValue);
}

BOOL CConfig::FileIsExist()
{
	CFile temp;
	return temp.Open(m_fileName,CFile::modeReadWrite);
}

